const connection = require('../app/database')

class UserService {
    // 创建用户
    async create(user) {
        try {
            const { name, password } = user
            const statement = `INSERT INTO user (name,password) VALUES (?,?);`
            const result = await connection.execute(statement, [name, password])
            return result[0]
        } catch (error) {
            console.log(error);
        }
    }

    // 通过用户名查找用户
    async getUserByName(name) {
        try {
            const statement = `SELECT * FROM user WHERE name=?;`
            const result = await connection.execute(statement, [name])
            return result[0]
        } catch (error) {
            console.log(error);
        }
    }

    // 更新用户头像
    async updateAvatarUrlById(avatarUrl, userId) {
        try {
            const statement = `UPDATE user SET avatar_url = ? WHERE id=?;`
            const [result] = await connection.execute(statement, [avatarUrl, userId])
            return result
        } catch (error) {
            console.log(error);
        }
    }
}

module.exports = new UserService()